var city = require('../../utils/city.js');
var app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
var cityList = city.cityList();
var cityObj = city.cityObj;
Page({
  data: {
    searchLetter: [],
    showLetter: "",
    winHeight: 0,
    cityList: [],
    isShowLetter: false,
    scrollTop: 0, //置顶高度
    scrollTopId: '', //置顶id
    city: "",
    hotcityList: [],
    fromType: "",
    searchValue: "",
    searchResult: [],
    isShowSearchRes:false
  },
  onLoad: function(options = {}) {
    this.data.fromType = options.fromtype;
    var searchLetter = city.searchLetter;
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    var itemH = winHeight / searchLetter.length;
    var tempObj = [];
    for (var i = 0; i < searchLetter.length; i++) {
      var temp = {};
      temp.name = searchLetter[i];
      temp.tHeight = i * itemH;
      temp.bHeight = (i + 1) * itemH;
      tempObj.push(temp)
    }
    this.setData({
      winHeight: winHeight,
      itemH: itemH,
      searchLetter: tempObj,
      cityList: cityList
    })
  },
  onShow: function() {
    this.setData({
      city: app.globalData.locateCity
    })
    server.postJSON(common.storePath + 'store/getopencity/', "", res => {
      if (res.data.errno == '0') {
        this.setData({
          hotcityList: res.data.data
        })
      } else {
        common.showErrorToast(res.errmsg)
      }
    })

  },
  searchByInput(e) {
    if (e.detail.value!=""){
      this.setData({
        searchValue: e.detail.value
      })
      var keyword = this.data.searchValue;
      var result = cityObj.filter((item, index) => {
        return (item.name).indexOf(keyword) > -1;
      })
      this.setData({
        searchResult: result,
        isShowSearchRes: true
      })
    }else{
      this.setData({
        isShowSearchRes: false
      })
    }
  },
  clearKeyword(){
    this.setData({
      searchValue:"",
      searchResult: [],
      isShowSearchRes: false
    })
  },
  clickLetter: function(e) {
    var showLetter = e.currentTarget.dataset.letter;
    this.setData({
      showLetter: showLetter,
      isShowLetter: true,
      scrollTopId: showLetter,
    })
    var that = this;
    setTimeout(function() {
      that.setData({
        isShowLetter: false
      })
    }, 1000)
  },
  //选择城市
  bindCity: function(e) {
    var city = e.currentTarget.dataset.city;
    var cityId = e.currentTarget.dataset.citycode;
    if (this.data.fromType == 'editaddr') {
      app.globalData.selectTempCity = city;
      app.globalData.selectTempCityId = cityId;
    } else if (this.data.fromType == 'searchaddr') {
      app.globalData.searchAddrCity = city;
    }
    if (this.data.searchValue!=""){
      this.setData({
        searchValue: "",
        searchResult: [],
        isShowSearchRes: false
      })
    }
    wx.navigateBack();
  },
  //点击热门城市回到顶部
  hotCity: function() {
    this.setData({
      scrollTop: 0,
    })
  }
})